/**
*@author Gabriel Rilo @ 01/06/2009
*/
public with sharing class UserPermissions {

	/* Vars */
	private String id;
	private String team;
	private String objectType;
	private String ownerId;
	private Boolean canComment = false;
	private Boolean canView = true;	
	private User teamAdmin = null;
	private List<DiscussionMember__c> members = null;
	private List<DiscussionForum__c> currentTeam = null;
	private DiscussionProfile__c profile = null;
	private Boolean canCreate = false;
	
	/* Booleans */
	public Boolean canManage = false;
	public static Boolean canManageStatic = false;

	
	/**
	* Costructor
	*/
	public UserPermissions (){}
	
	/**
	*@return String
	*/
	public String getId () {
		return id;
	}
	
	/**
	*@return String
	*/
	public String getTeam () {
		return team;
	}
	
	/**
	*@return String
	*/
	public String getObjectType () {
		return objectType;
	}
	
	/**
	*@return Boolean
	*/
	public Boolean getCanCreate () {
		return canCreate;
	}
	
	/**
	*@return Boolean
	*/
	public Boolean getCanManage () {
		return canManage;
	}
	
	/**
	*@return Boolean
	*/
	public static Boolean getCanManageStatic () {
	
		return canManageStatic;
	}
	
	/**
	*@return Boolean
	*/
	public Boolean getCanComment () {
		return canComment;
	}
	
	/**
	*@return Boolean
	*/
	public Boolean getCanView () {
		return canView;
	}
	
	/**
	*@param String
	*/
	public void setId (String id) {
		this.id = id;
	}
	
	/**
	*@param String
	*/ 	
 	public void setOwnerId (String ownerId) {
		this.ownerId = ownerId;
	}
	
	/**
	*@param String
	*/	
	public void setTeam (String team) {
		this.team = team;
	}
	
	/**
	*@param String
	*/	
	public void setObjectType (String objectType) {
		this.objectType = objectType;
	}
	
	/**
	* Get permissions for current user
	*@throws Exception
	*/
	public void setPermissions () {
		try{
			canManage = false;
			canCreate = false;
			canComment = false;
			Boolean isAdmin = false;			
			if(teamAdmin == null || teamAdmin.Id != UserInfo.getUserId())
				teamAdmin = [Select id, Profile.PermissionsModifyAllData, ProfileId, Name From User where id =: id limit 1];
			if(teamAdmin.Profile.PermissionsModifyAllData){
				canManage = true;
				canCreate = true;
				canView = true;
				canComment = true;
				isAdmin = true;
			}
			if(!isAdmin){
				if(members == null){
					members = new List<DiscussionMember__c>();
					members = [select DiscussionProfile__r.CreateDiscussionTopics__c, 
	 								  DiscussionProfile__r.PostDiscussionTopicReplies__c,
		    						  DiscussionProfile__r.ManageDiscussionForums__c, 
									  User__c
							  from DiscussionMember__c 
							  where User__c =:id 
							  and DiscussionForum__c=:Team 
							  limit 1];
				}
			
				List<DiscussionForum__c> myWiki = new List<DiscussionForum__c>();
				myWiki = [select Type__c from DiscussionForum__c where id=:Team limit 1];
				if (members.size() > 0) {
					profile = members[0].DiscussionProfile__r;
				// If the user is not a member of the team then we have to go to the public profile of the team
				// to figure out the permissions.
				}else{
					if(currentTeam == null){
						System.debug('Lcosta currentTeam is null');
						List<DiscussionForum__c> currentTeam = new List<DiscussionForum__c>();
						currentTeam = [select PublicProfile__c, 
													 	Id,
													 	PublicProfile__r.CreateDiscussionTopics__c, 
													 	PublicProfile__r.PostDiscussionTopicReplies__c,
													 	PublicProfile__r.ManageDiscussionForums__c 
											  		 from DiscussionForum__c
											  		 where Id =: Team];
										
						if(currentTeam.size() > 0){ 
							profile = currentTeam[0].PublicProfile__r;
						}
					}
				}
				if (profile != null) {
					if (objectType == 'DiscussionForum' || objectType == 'Discussion') {
						if(profile.CreateDiscussionTopics__c){
							canCreate = true;
						}
						if(profile.PostDiscussionTopicReplies__c){
							canComment = true;
						}
						if (profile.ManageDiscussionForums__c){
							canManage = true;
							canCreate = true;
							canComment = true;
						}					
					}	
				}
			}
			
			String canManageStr = (canManage) ? 'true' : 'false';
		
			if(ownerId == id) 
				canManage = true;  // Shouldnt this be called user UserId?
				
			canManageStatic = canManage;
			
		}catch(Exception e){
			throw e;
		}
	}
}